package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存
    public R save(@RequestBody SetmealDto setmealDto){
        setmealService.save(setmealDto);
        return R.success(null);
    }
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<SetmealDto> pageInfo = setmealService.page( page, pageSize, name);
        return R.success(pageInfo);
    }
    /*
        删除套餐
        注意事项：集合接收数据，必须使用@RequestParam
     */
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存
    @DeleteMapping
    public R removeByIds(@RequestParam List<Long> ids){
        setmealService.removeByIds(ids);
        return R.success(null);
    }
    /*
        根据分类id查询套餐
        dish_分类id_1
        setmeal_分类id_1
     */
    @Cacheable(value = "setmealCache",key ="'setmeal_'+#categoryId+'_'+#status" )
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,categoryId);
        setmealLambdaQueryWrapper.eq(status!=null,Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dishs(@PathVariable("id") Long setmealId){
        List<DishDto> dishDtoList = setmealService.dishs(setmealId);
        return R.success(dishDtoList);
    }

}
